package server;


import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.jdo.Query;

import webWof.State;
import webWof.StrippedGameDB;
import webWof.WebPlayer;


public class GameDBQueryServlet extends HttpServlet {

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GameDBQueryServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String query;
		PersistenceManager pm = PMF.get().getPersistenceManager();

		try {
			response.setContentType("application/x-java-serialized-object");

			InputStream in = request.getInputStream();
			ObjectInputStream inputFromApplet = new ObjectInputStream(in);
			java.lang.Object o =  inputFromApplet.readObject();
			String q = (String)o;
			ArrayList<StrippedGameDB> resault = null;
			OutputStream outstr = response.getOutputStream();
			ObjectOutputStream oos = new  ObjectOutputStream(outstr);
			resault=WebGamesManager.getInstance().getGameHistory(q);
			oos.writeObject(resault);
			oos.flush();
			oos.close();

			/*
			 query = (String) o;
			Query q1 = pm.newQuery(query);
			ArrayList<PlayerDB> results = (ArrayList<PlayerDB>) q1.execute(State.PENDING);

			OutputStream outstr1 = response.getOutputStream();
			ObjectOutputStream oos1 = new  ObjectOutputStream(outstr1);
			oos1.writeObject(results);
			oos1.flush();
			oos1.close();
			 */
		} catch (Exception e) { 
			e.printStackTrace();
		} finally {
			pm.close();
		}
	}


	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}



